package zhongbo.java.air.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.auth.mapper.UserMapper;
import zhongbo.java.air.auth.model.User;
import zhongbo.java.air.auth.service.IUserService;
import zhongbo.java.air.common.vo.PageVo;

import java.util.Date;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年03月29日 星期六 12:20:24
 */
@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User add(User user) {
        validUser(user);
        // 新增用户 密码 不能为空
        String passWord = user.getPassWord();
        if (StrUtil.isBlank(passWord)) {
            throw new RuntimeException("用户密码不能为空");
        }
        // 默认设置为不是管理员
        if (user.getIsAdmin() == null) {
            user.setIsAdmin(false);
        }
        // 新增用户不允许指定id
        user.setUserId(null);

        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return user;
        }
        throw new RuntimeException("新增用户失败");
    }

    /**
     * 通用验证用户信息
     */
    private void validUser(User user) {
        if (user == null) {
            throw new RuntimeException("用户信息不能为空");
        }
        String userName = user.getUserName();
        if (StrUtil.isBlank(userName)) {
            throw new RuntimeException("用户名称不能为空");
        }
    }

    @Override
    public String delete(String userId) {
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("用户id不能为空");
        }
        int delete = userMapper.deleteById(userId);
        if (delete > 0) {
            return userId;
        }
        throw new RuntimeException("删除用户失败");
    }

    @Override
    public User edit(User edit) {
        validUser(edit);
        String userId = edit.getUserId();
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("用户id不能为空");
        }
        User one = findOne(userId);
        if (one == null) {
            throw new RuntimeException("用户不存在");
        }
        String oldUserName = one.getUserName();
        if (!edit.getUserName().equals(oldUserName)) {
            throw new RuntimeException("用户名不能修改");
        }
        edit.setUpdateTime(new Date());
        int update = userMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改用户失败");
    }

    @Override
    public User findOne(String userId) {
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("用户id不能为空");
        }
        return userMapper.selectById(userId);
    }

    @Override
    public PageVo<User> findPage(String userName, Integer page, Integer pageSize, String oderByField, Boolean orderByAsc) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(userName)) {
            // 模糊查询用户名
            queryWrapper.like("user_name", userName);
        }
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<User> pageParam = new Page<>(page, pageSize);
        Page<User> userPage = userMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(userPage);
    }

    @Override
    public User findByUserNameAndPassWord(String userName, String passWord) {
        validUserName(userName);
        validPassWord(passWord);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        queryWrapper.eq("pass_word", passWord);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findByUserName(String userName) {
        validUserName(userName);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        return userMapper.selectOne(queryWrapper);
    }

    private void validPassWord(String passWord) {
        if (StrUtil.isBlank(passWord)) {
            throw new RuntimeException("密码不能为空");
        }
    }

    private void validUserName(String userName) {
        if (StrUtil.isBlank(userName)) {
            throw new RuntimeException("用户名不能为空");
        }
    }

}
